<template>
  <div class="dialog-form">
    <el-dialog v-model="formVisible" class="elDialogBox" title="填写取消原因" @close="onClickCloseBtn">
      <!-- 表单内容 -->
      <div class="dialogCenter">
        <el-form ref="queryRef" :model="formData" :rules="rules" label-width="108px">
          <el-form-item label="取消原因：" name="reason">
            <el-input v-model="formData.reason" :maxlength="100" class="wt-400"
                      placeholder="请输入" resize="none" show-word-limit type="textarea">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="onClickCloseBtn">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import {ref, watch} from 'vue';

const {proxy} = getCurrentInstance();

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});

const emit: Function = defineEmits(['handleClose', 'handleEditForm']);
const resetType = ref('empty');
const form = ref();
const formVisible = ref(false);

const formData = ref<Object | any>({
  reason: '',
});

const rules = {
  reason: [
    {
      required: true,
      message: '取消原因为空，请输入取消原因',
      trigger: 'blur',
    }
  ]
};

const pageTitle = ref();

watch(
    () => props.visible,
    () => {
      formVisible.value = props.visible;
      pageTitle.value = props.title;
    }
);

// 提交表单
const submitForm = () => {
  proxy.$refs['queryRef'].validate((valid) => {
    if (valid) {
      emit('handleEditForm', formData.value);
      handleClear()
    }
  });
};

// 清除表单数据
const handleClear = () => {
  proxy.resetForm('queryRef');
};

const onClickCloseBtn = () => {
  handleClear();
  emit('handleClose');
};
defineExpose({
  handleClear,
});
</script>
